// Entry point of calendar. 
// It also acts as the connection point of calendar header/body and other components
function CalendarApplication(repository, calendarHeaderStatus) {
    this._repository = repository;
    this._settingRepository = new CalendarSettingRepository();
    this._calendarViewBody = new CalendarViewBody($('#calendar-body'));
    this._calendarViewHeader = new CalendarViewHeader($('#calendar-month-header'), this._repository, calendarHeaderStatus);
    this._overviewHeader = new CalendarOverviewHeader($('#calendar-overview-header'), this._repository, calendarHeaderStatus);
    this._overviewBody = new CalendarOverviewBody($('#overview-locations'),$('#overview-activities'), this._repository);
    this._calendar = new Calendar(this._repository, this._calendarViewBody);
    this._controllers = [];
    this._init();
    this._currentMode = 'fill';
}

CalendarApplication.prototype = {
    show: function() {
        this._calendarViewHeader.show();
        this._registerControllers();
    },
    changeToOverviewMode: function() {
        this._currentMode = 'overview';
        $('#overview').show();
        $('#calendar').hide();
        this._overviewHeader.show();
    },
    changeToFillMode: function() {
        if (this._currentMode == 'fill')
        { return; }
        $('#overview').hide();
        $('#calendar').show();
        this._currentMode = "fill";
        this._calendarViewHeader.show();
    },
    _init: function() {
        var me = this;
        this._overviewHeader.onHeaderStatusChange(function() {
            me._overviewBody.update();
        });

        this._calendarViewHeader.onHeaderStatusChange(function() {
            var headerStatus = me._calendarViewHeader.getHeaderStatus();
            var showingMonth = me._calendar.getShowingYearMonth();

            if (!showingMonth || !headerStatus.selectedYearMonth.isSameWith(showingMonth)) {
                me._calendar.showMonth(headerStatus.selectedYearMonth);
            }

            ///TODO: wyh - is it easier to use setTimeout here? (instead of setting repository)
            me._settingRepository.saveLastVisitedMonth(headerStatus);
        });
        this._calendar.onRefresh(function() {
            me._calendarViewHeader.updateMonthStatus();
        });
    },

    _registerControllers: function() {
        var undoButton = new Button($('#calendar_undo'));
        var handIcon = new Icon($('#calendar_view'));
        var activityEraseIcon = new Icon($('#activity_eraser'));
        var locationEraseIcon = new Icon($('#location_eraser'));
        // Hide help icon for release 2
        //var helpIcon = new Icon($('#calendar_help'));

        var workIcon = new Icon($('#calendar_activity_work'), DayActivity.work);
        var nonWorkIcon = new Icon($('#calendar_activity_non_work'), DayActivity.other);
        var sickIcon = new Icon($('#calendar_activity_sick'), DayActivity.sick);
        var vacationIcon = new Icon($('#calendar_activity_vacation'), DayActivity.vacation);
        var nationalHolidayIcon = new Icon($('#calendar_activity_national_holiday'), DayActivity.national);

        var basicIcons = [handIcon, activityEraseIcon, locationEraseIcon];
        var activityIcons = [workIcon, nonWorkIcon, sickIcon, vacationIcon, nationalHolidayIcon];
        var controlIcons = basicIcons.concat(activityIcons);

        // Hide help icon for release 2
        //var helpDialog = new HelpDialog($('#calendar_help_container'));
        var dayDetailDialog = new DayDetailDialog($('#day_detail_dialog_content'));
        var calendarHeaderTab = $('.calendar-header-tabview');
        var locationTabDom = $("#location_tab");
        var locationTabView = new LocationTabView(locationTabDom, [handIcon, locationEraseIcon]);
        var downloadPDFView = new DownloadPDFView();


        this._registerController(new UndoController(undoButton));
        // Hide help icon for release 2 
        //this._registerController(new HelpController(helpDialog, helpIcon));
        this._registerController(new DayDetailController(this._calendarViewBody, dayDetailDialog, handIcon, this._repository));
        this._registerController(new ActivityEraserController(this._calendarViewBody, activityEraseIcon));
        this._registerController(new LocationEraserController(this._calendarViewBody, locationEraseIcon));
        this._registerController(new ActivityController(this._calendarViewBody, activityIcons));
        this._registerController(new CalendarTabController(this._calendarViewBody, calendarHeaderTab, handIcon, controlIcons));
        this._registerController(new LocationTabController(this._calendarViewBody, locationTabView));
        this._registerController(new DownloadPDFController(this._calendarViewHeader, downloadPDFView, this._repository));
        this._registerController(new OverviewTabController(this, $('#overview_tabitem'), [$('#location_tabitem'), $('#activity_tabitem')]));
        this._registerController(new OverviewBodyController(this._overviewBody, this._overviewHeader, this._repository));
    },
    _registerController: function(controller) {
        controller.register(this._calendar);
        this._controllers.push(controller);
    }
};
